package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.commondata.feast.FeastConstant;
import com.motu.monstercity.server.game.commondata.feast.FeastCreaterReward;
import com.motu.monstercity.server.game.commondata.feast.FeastJoin;
import com.motu.monstercity.server.game.commondata.feast.FeastMain;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class FeastBaseData {
    private static Logger logger = LoggerFactory.getLogger(FeastBaseData.class);
    private static ConcurrentHashMap<Integer, FeastMain> feastMainMap = new ConcurrentHashMap<>(); // 派对配置表
    private static ConcurrentHashMap<Integer, FeastJoin> feastJoinMap = new ConcurrentHashMap<>(); // 派对参与奖励配置表
    private static ConcurrentHashMap<Integer, FeastCreaterReward> feastCreaterRewardMap = new ConcurrentHashMap<>(); // 派对举办者的奖励


    // 公共数据初始化
    public static void init(List<Future> futures) {
        futures.add(initFeastMain());
        futures.add(initFeastJoin());
        futures.add(initFeastCreaterReward());
    }

    /**
     * 初始化派对配置表
     */
    public static Future<Void> initFeastMain() {
        String table = "feast_main";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, FeastMain> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        FeastMain entity = new FeastMain(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    feastMainMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static FeastMain getFeastMain(int id) {
        FeastMain result = feastMainMap.get(id);
        if (result == null) {
            logger.error("getFeastMain error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, FeastMain> getFeastMainMap() { return feastMainMap; }

    /**
     * 初始化派对参与奖励配置表
     */
    public static Future<Void> initFeastJoin() {
        String table = "feast_join";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, FeastJoin> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        FeastJoin entity = new FeastJoin(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    feastJoinMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static FeastJoin getFeastJoin(int id) {
        FeastJoin result = feastJoinMap.get(id);
        if (result == null) {
            logger.error("getFeastJoin error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, FeastJoin> getFeastJoinMap() { return feastJoinMap; }

    // 根据派对类型,参加类型获取feastjoin配置
    public static FeastJoin getFeastJoin(int feastType, int joinType) {
        for (FeastJoin feastJoin : getFeastJoinMap().values()) {
            if (feastJoin.getFeastType() == feastType && feastJoin.getJoinType() == joinType) {
                return feastJoin;
            }
        }
        return null;
    }

    /**
     * 初始化派对举办者的奖励
     */
    public static Future<Void> initFeastCreaterReward() {
        String table = "feast_creater_reward";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, FeastCreaterReward> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        FeastCreaterReward entity = new FeastCreaterReward(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    feastCreaterRewardMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static ConcurrentHashMap<Integer, FeastCreaterReward> getFeastCreaterRewardMap() { return feastCreaterRewardMap; }

    // 根据人数获取FeastCreaterReward
    public static FeastCreaterReward getFeastCreaterReward(int num) {
        for (FeastCreaterReward feastCreaterReward : getFeastCreaterRewardMap().values()) {
            if (num >= feastCreaterReward.getNumMin() && num <= feastCreaterReward.getNumMax()) {
                return feastCreaterReward;
            }
        }
        return null;
    }

    // 获取机器的赞助类型id
    public static FeastJoin getRobotJoin() {
        for (FeastJoin feastJoin : getFeastJoinMap().values()) {
            if (feastJoin.getFeastType() == FeastConstant.FEAST_TYPE_1 && feastJoin.getJoinType() == AllParam.FEAST_ROBOT_JOIN_TYPE) {
                return feastJoin;
            }
        }
        return null;
    }
}
